
var eventIdArr=[];

$(function () {
    // 初始化数据
    initData();
    // 动态调整列宽
    $("table").colResizable();
    // 关闭按钮
    $(".press_btn2").click(function () {
        closeWindows();
    });
    
    // 解绑事件
    $(".press_btn1").click(function () {
        unLinktoEvents();
    });

});

function initData() {
    pageEvents(1, 15);
}

function pageEvents(pageNo, pageSize) {
    $("#eventsTable").empty();
    var columns = [{code:"eventId", name:"checkBox"},
        {code:"eventSort", name:"事件分类"},
        {code:"eventType", name:"事件分级"},
        {code:"threatLevelStr", name:"威胁级别"},
        {code:"eventStateStr", name:"事件状态"}];
    var params = {url:basePath + "/splunk/alert/getEventsBySid.do", divID:"eventsTable",afterQuery:"adjustTable", message:"该告警暂时没有绑定的事件",isAsync:true};
    var data = {pageNo: pageNo, pageSize: pageSize, alertSid:alertSid};
    easyQuery(columns, params, data);
}

// 告警分页加载完后，将选中的告警信息回显
function adjustTable() {
    // 给表格添加点击事件
    $("#eventsTable tr").each(function (i, n) {
        if (i != 0) {
            $(this).find("td[class!='operation']").click(function () {
                var dom = $(this).parents("tr").find("td[class='checkNot']").find("input");
                if ($(dom).is(":checked")) {
                    $(dom).prop("checked", false);
                }else{
                    $(dom).prop("checked", "checked");
                }
                manucheck(dom);
            });
        }
    })
}

function unLinktoEvents() {
    if (eventIdArr.length == 0) {
        layer.msg("请先勾选要解绑的事件", {time: 1500});
        return;
    }
    $.ajax({
        url: basePath + "/splunk/alert/unLinkEvents.do",
        type: "post",
        dataType: "json",
        data: {
            eventIds: eventIdArr.join(","),
            alertSid:alertSid,
        },
        success:function (json) {
            var code = json.code;
            if (code == '200') {
                parent.layer.msg("事件解绑成功", {time: 1500}, function () {
                    closeWindows();
                    parent.alertSidArr = []; // 清空全局数组
                    parent.initData();
                });
            }else if (code == '500'){
                parent.layer.msg("事件解绑失败", {time: 1500});
            }
        }
    });
}

// table页 第一列单选框触发事件
function manucheck(input) {
    // 将选中的sid放到全局中
    if ($(input).is(":checked")){
        eventIdArr.push($(input).attr("nodeid"));
        $(input).parents("tr").addClass("timeoutTr");
    }else {
        var eventId = $(input).attr("nodeid");
        eventIdArr = $.grep(eventIdArr, function (value) {
            return value != eventId
        });
        $(input).parents("tr").removeClass("timeoutTr");
    }
}

// 全选按钮
function afterSelectAll(input) {
    eventIdArr = [];
    if ($(input).is(":checked")) {
        $("#eventsTable tr").each(function (i, tr) {
            if (i != 0) {
                eventIdArr.push($(this).attr("nodeid"));
                $(this).addClass("timeoutTr");
            }
        })
    }else {
        $("#eventsTable tr").each(function (i, tr) {
            if (i != 0) {
                $(this).removeClass("timeoutTr");
            }
        })
    }
}

function closeWindows() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}